library(texreg)
library(lme4)
library(lmerTest)
library(interplot)
library(sjPlot)
library(tidyverse)
library(sjstats)
library(plm)
library(clubSandwich)
library(marginaleffects)
library(Hmisc)
library(gtsummary)

#Load data
load("coalition_history_dataset.Rdata")
load("coalitionhistory_EJPR_RR.RData")

#Models
#Fixed Effects
dmodel1fe <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                  + party_age + prime_minister  + seatshare 
                  + age  + education + both_coalition + exclude
                  + both_coalition*coalition_decay_95_60
                  + exclude*coalition_decay_95_60
                  + factor(country_name_short) - 1
                  + (1 | party_dyad_cabinet_id), data = dyadic_parlgov_CSES_new)
summary(dmodel1fe)


#No Squared Term
dmodel1_nosq <- lmer(per_dist ~ coalition_decay_95_60 + rile_dist
                     + party_age + prime_minister  + seatshare 
                     + age  + education + both_coalition + exclude
                     + both_coalition*coalition_decay_95_60
                     + exclude*coalition_decay_95_60
                     + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel1_nosq)

#No Single Election cases
dyadic_nosingle <- dyadic_parlgov_CSES_new %>%
  filter(!country_name_short %in% c("FRA", "EST", "HRV"))

model_nosingle <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                       + party_age + prime_minister  + seatshare 
                       + age  + education + both_coalition + exclude
                       + both_coalition*coalition_decay_95_60
                       + exclude*coalition_decay_95_60
                       + (1 | party_dyad_cabinet_id) + (1| country_name_short), 
                       data = dyadic_nosingle)
summary(model_nosingle)


#Just Dyads
dyadic_dyads <- dyadic_parlgov_CSES_new %>%
  unite(dyad_cabinet_id, c(dyad_id, cabinet_id), remove=F)

dyadic_dyads <- dyadic_dyads %>%
  subset(select = c(per_dist, coalition_decay_95_60, coalition_decay_95_60_sq, rile_dist, age, 
                    education, both_coalition, exclude, dyad_election_id, country_name_short, 
                    dyad_id, election_id))%>%
  unique()

model_dyad <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                   #+ party_age + prime_minister  + seatshare 
                   + age  + education + both_coalition + exclude
                   + both_coalition*coalition_decay_95_60
                   + exclude*coalition_decay_95_60
                   + (1 | dyad_cabinet_id) + (1|country_name_short), data = dyadic_dyads)
summary(model_dyad)


#Count with duration
load("coalition_history_dataset.Rdata")
fmodel1 <- lmer(per_dist ~ prop_month_99 + prop_month_99_sq + rile_dist + party_age 
                + prime_minister + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*prop_month_99
                + exclude*prop_month_99
                + (1 | party_dyad_election_id) + (1|country_name_short), data = dyadic_parlgov_familiarity)
summary(fmodel1)


ymodel1 <- lmer(per_dist ~ coalition_year_99_50 + coalition_year_99_50_sq + rile_dist 
                + party_age + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*coalition_year_99_50
                + exclude*coalition_year_99_50
                + (1 | party_dyad_election_id) + (1|country_name_short), data = dyadic_parlgov_familiarity)
summary(ymodel1)



#Different Decay Rates
dmodel3 <- lmer(per_dist ~ coalition_decay_99_60 + coalition_decay_99_60_sq + rile_dist 
                + party_age + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*coalition_decay_99_60
                + exclude*coalition_decay_99_60
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel3)



dmodel2 <- lmer(per_dist ~ coalition_decay_9_60 + coalition_decay_9_60_sq +rile_dist 
                + party_age + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*coalition_decay_9_60
                + exclude*coalition_decay_9_60
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel2)


dmodel5 <- lmer(per_dist ~ coalition_decay_8_60 + coalition_decay_8_60_sq +rile_dist 
                + party_age + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*coalition_decay_8_60
                + exclude*coalition_decay_8_60
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel5)

dmodel6 <- lmer(per_dist ~ coalition_decay_75_60 + coalition_decay_75_60_sq +rile_dist 
                + party_age + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*coalition_decay_75_60
                + exclude*coalition_decay_75_60
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel6)

dmodel10 <- lmer(per_dist ~ coalition_decay_55_60 + coalition_decay_55_60_sq +rile_dist 
                 + party_age + prime_minister  + seatshare 
                 + age  + education + both_coalition + exclude
                 + both_coalition*coalition_decay_55_60
                 + exclude*coalition_decay_55_60
                 + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel10)

dmodel11 <- lmer(per_dist ~ coalition_decay_5_60 + coalition_decay_5_60_sq +rile_dist 
                 + party_age + prime_minister  + seatshare 
                 + age  + education + both_coalition + exclude
                 + both_coalition*coalition_decay_5_60
                 + exclude*coalition_decay_5_60
                 + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel11)

#Count without decay
model10 <- lmer(per_dist ~ prev_coalition_10yr + prev_coalition_10yr_sq + rile_dist + party_age 
                + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*prev_coalition_10yr
                + exclude*prev_coalition_10yr
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(model10)
class(model10) <- "lmerMod"

model20 <- lmer(per_dist ~ prev_coalition_20yr + prev_coalition_20yr_sq + rile_dist 
                + party_age + prime_minister  + seatshare 
                + age  + education + both_coalition + exclude
                + both_coalition*prev_coalition_20yr
                + exclude*prev_coalition_20yr
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(model20)

model30 <- lmer(per_dist ~ prev_coalition_30yr + prev_coalition_30yr_sq + rile_dist 
                + party_age + prime_minister  + seatshare 
                + age + education + both_coalition + exclude 
                + both_coalition*prev_coalition_30yr
                + exclude*prev_coalition_30yr
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(model30)

#Binary History
model20_bi <- lmer(per_dist ~ prev_coalition_binary_20yr + rile_dist + party_age + prime_minister  + seatshare 
                   + age + education + both_coalition + exclude
                   + both_coalition*prev_coalition_binary_20yr
                   + exclude*prev_coalition_binary_20yr
                   + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(model20_bi)

#Consecutive Count
model28 <- lmer(per_dist ~ lag_consecutive_both + lag_consecutive_both_sq + rile_dist + party_age 
                + prime_minister  + seatshare 
                + age + education + both_coalition + exclude
                + both_coalition*lag_consecutive_both
                + exclude*lag_consecutive_both
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(model28)

model29 <- lmer(per_dist ~ lag_consecutive_same + lag_consecutive_same_sq + rile_dist 
                + party_age + prime_minister  + seatshare 
                + age + education + both_coalition + exclude
                + both_coalition*lag_consecutive_same
                + exclude*lag_consecutive_same
                + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(model29)

#Previous Partner Count
dmodel1_prevpartners <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                             + party_age + prime_minister  + seatshare + prev_partner_count
                             + age  + education + both_coalition + exclude
                             + coalition_decay_95_60*prev_partner_count
                             + both_coalition*coalition_decay_95_60
                             + exclude*coalition_decay_95_60
                             + (1 | party_dyad_cabinet_id) + (1|country_name_short), data = dyadic_parlgov_CSES_new)
summary(dmodel1_prevpartners)

#Previous Partner Proportion
dmodel1_prev_prop <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                          + party_age + prime_minister  + seatshare + prop_prev_partner
                          + age  + education + both_coalition + exclude
                          + coalition_decay_95_60*prop_prev_partner
                          + both_coalition*coalition_decay_95_60
                          + exclude*coalition_decay_95_60
                          + (1 | party_dyad_cabinet_id) + (1|country_name_short), 
                          data = dyadic_parlgov_CSES_new)
summary(dmodel1_prev_prop)

#Current Partner Cabinet Size
dmodel1_cabnum <- lmer(per_dist ~ coalition_decay_95_60 + coalition_decay_95_60_sq + rile_dist 
                       + party_age + prime_minister  + seatshare + cabinetnumber
                       + age  + education + both_coalition + exclude
                       + both_coalition*cabinetnumber
                       + both_coalition*coalition_decay_95_60
                       + exclude*coalition_decay_95_60
                       + (1 | party_dyad_cabinet_id) + (1|country_name_short), 
                       data = dyadic_parlgov_CSES_new)
summary(dmodel1_cabnum)

#Appendix
texreg(dmodel1fe)
performance::icc(dmodel1fe)
texreg(dmodel1_nosq)
performance::icc(dmodel1_nosq)
texreg(model_nosingle)
performance::icc(model_nosingle)
texreg(model_dyad)
performance::icc(model_dyad)
texreg(fmodel1)
performance::icc(fmodel1)
texreg(ymodel1)
performance::icc(ymodel1)
texreg(c(dmodel3, dmodel2, dmodel5))
performance::icc(dmodel2)
performance::icc(dmodel3)
performance::icc(dmodel5)
texreg(c(dmodel6, dmodel10, dmodel11))
performance::icc(dmodel6)
performance::icc(dmodel10)
performance::icc(dmodel11)
texreg(c(model10,model20,model30))
performance::icc(model10)
performance::icc(model20)
performance::icc(model30)
texreg(c(model20_bi))
performance::icc(model20_bi)
texreg(model28)
performance::icc(model28)
texreg(model29)
performance::icc(model29)
texreg(dmodel1_prevpartners)
performance::icc(dmodel1_prevpartners)
texreg(dmodel1_prev_prop)
performance::icc(dmodel1_prev_prop)
texreg(dmodel1_cabnum)
performance::icc(dmodel1_cabnum)